package com.example.car

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.car.adapter.ParkingAdapter
import com.example.car.databinding.ActivityParkingListBinding
import com.example.car.vm.ParkingViewModel
import com.example.common.view.view.BaseMvvmActivity
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/car/ParkingListActivity")
@AndroidEntryPoint
class ParkingListActivity : BaseMvvmActivity<ActivityParkingListBinding>() {
    override fun getLayoutId(): Int  =  R.layout.activity_parking_list
    val parkingAdapter = ParkingAdapter()
    var areaId = 1


    private val parkingViewModel : ParkingViewModel by viewModels()

    override fun initView() {
        super.initView()
        mDataBinding.back.setOnClickListener {
            finish()
        }

        mDataBinding.rv.adapter = parkingAdapter
        mDataBinding.rv.layoutManager = LinearLayoutManager(this)

        //获取车场列表
        parkingViewModel.getParking(areaId)
        parkingViewModel.mSuccessLiveData.observe(this){
            ToastUtils.showLong("获取成功")
            parkingAdapter.setList(it.data)
            parkingAdapter.notifyDataSetChanged()
        }
        parkingViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }

        parkingAdapter.setOnItemClickListener { adapter, view, position ->
            val parkingEntity = parkingAdapter.getItem(position)
            ARouter.getInstance().build("/car/ParkingEditActivity")
                .withParcelable("parkingEntity",parkingEntity)
                .navigation()
        }
        mDataBinding.add.setOnClickListener {
            ARouter.getInstance().build("/car/ParkingEditActivity").navigation()
        }
    }

    override fun onResume() {
        super.onResume()
        parkingViewModel.getParking(areaId)
    }

}